import axios from 'axios'
import apiConfig from '@/config/http'
import router from '@/router'

// 全局默认配置
// const user = JSON.parse(localStorage.getItem('user')) || {}
// axios.defaults.headers.common['authorization'] = user.token

const http = axios.create({
  baseURL: '/api',
  timeout: apiConfig.timeout
})

// axios请求拦截器
http.interceptors.request.use(config => {
  /* console.log('本次请求地址为',config.url); */
  const admin = JSON.parse(localStorage.getItem('admin')) || {}
  config.headers.authorization = admin.token
  return config
})

// axios响应拦截器
http.interceptors.response.use(response => {
  // 登录失效后的处理
  /* console.log('本次响应地址为',response.config.url); */
  if (response.data.code === 403) {
    localStorage.removeItem('admin')
    router.replace('/userlogin')
  }
  return response.data
})

export default http
